import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/pages/common/BaseMaterialInfo.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/pages/common/TextFieldItem.dart';
import 'package:xiongan_app_yy/pages/common/TextItemCard.dart';
import 'package:xiongan_app_yy/provider/autopicking_provider.dart';
import 'package:xiongan_app_yy/utils/image_utils.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';
import 'package:xiongan_app_yy/widgets/comm/number_count_button.dart';

class AutoPickingDetail extends StatefulWidget {
  String id;
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _AutoPickingDetail();
  }

  AutoPickingDetail(this.id);
}

class _AutoPickingDetail extends State<AutoPickingDetail> {
  Map<String, dynamic> map = Map();
  String buttonText = "下架";
  bool load = false;
  bool flag = false;
  bool shelfState = false; //是否下架成功,也就是拣选取货完成
  TextEditingController controller0 = TextEditingController();
  TextEditingController controller1 = TextEditingController();
  TextEditingController controller2 = TextEditingController();
  Widget button;
  Widget button0;
  String image = "assets/images/build.png";

  @override
  void initState() {
    super.initState();
   /* WidgetsBinding.instance.addPostFrameCallback((_) async {
      if (map["ispickup"] == true) {
        shelfState = map["ispickup"];
        controller0.text = map["quantity"].toInt().toString();
        controller1.text = map["tolocationcode"] ?? "";
      } else {
        controller0.text = map["quantity"].toInt().toString();
        controller1.text = map["tolocationcode"] ?? "";
      }
      setState(() {});
    });*/
  }

  @override
  Widget build(BuildContext context) {
    map = Provider.of<AutoPickingProvider>(context).detailData;
     if(!flag){
      if(map["ispickup"]==true){
        shelfState=map["ispickup"];
        controller0.text=map["quantity"].toInt().toString();
        controller1.text=map["tolocationcode"]??"";
      }else{
        controller0.text=map["quantity"].toInt().toString();
        controller1.text=map["tolocationcode"]??"";
      }
      setState(() {
        flag=true;
      });
    }

    if (map["imageurl"] != null && image == "assets/images/build.png") {
      ImageDownloadUtil.getImageBase64(map["imageurl"], (im) {
        setState(() {
          image = im;
        });
      });
    }
    if (map["ispickup"] == true) {
      shelfState = map["ispickup"];
    }
    if (map["issendputawaytask"] == false) {
      button = Expanded(
          child: FlatButton(
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(0))),
        padding: EdgeInsets.all(15),
        color: Color(0xFF42AFF0),
        child: Text(
          "上架",
          style: TextStyle(color: Colors.white),
        ),
        onPressed: () {
          if (shelfState == true) {
            setState(() {
              load = true;
            });
            Provider.of<AutoPickingProvider>(context)
                .sendOrderAutoPutqwayTask(sendObj: map)
                .then((a) {
              if (a == true) {
                setState(() {
                  load = false;
                });
                Provider.of<AutoPickingProvider>(context)
                    .getDetailListData(orderid: widget.id);
                Navigator.pop(context, true);
              }else{
                setState(() {
                  load = false;
                });
              }
            });
          } else {
            TostUtil.showToast("请先下架");
          }
        },
      ));
    } else {
      button = Container();
    }

    if (map["issendputdowntask"] == false) {
      button0 = Expanded(
          child: FlatButton(
        color: Color(0xFF42AFF0),
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(0))),
        padding: EdgeInsets.all(15),
        child: Text(
          buttonText,
          style: TextStyle(color: Colors.white),
        ),
        onPressed: () {
          if (map["devicetypeid"] == "1") {
            if (controller0.text.length > 0) {
              if (controller1.text.length > 0) {
                setState(() {
                  load = true;
                });
                map["quantity"] =  int.parse(controller0.text);
                map["tolocationcode"] = controller1.text;
                Provider.of<AutoPickingProvider>(context)
                    .sendOrderTakeMaterialTask(sendObj: map)
                    .then((a) {
                  if (a == true) {
                    Provider.of<AutoPickingProvider>(context)
                        .getDetailListData(orderid: widget.id);
                    setState(() {
                      shelfState = true;
                      load = false;
                    });
                    if (map["issendputawaytask"] == true) {
                      Navigator.pop(context, true);
                    }
                  }else{
                    setState(() {
                      load = false;
                    });
                  }
                });
              } else {
                TostUtil.showToast("请扫描或输入下架位置");
              }
            } else {
              TostUtil.showToast("请输入实际拣选数");
            }
          } else {
            if (controller0.text.length > 0) {
              setState(() {
                load = true;
              });
              Provider.of<AutoPickingProvider>(context)
                  .sendOrderTakeMaterialTask(sendObj: map)
                  .then((a) {
                if (a == true) {
                  print("请求新数据");
                  Provider.of<AutoPickingProvider>(context)
                      .getDetailListData(orderid: widget.id);
                  setState(() {
                    shelfState = true;
                    load = false;
                  });
                  if (map["issendputawaytask"] == true) {
                    Navigator.pop(context, true);
                  }
                }
              });
            } else {
              TostUtil.showToast("请输入实际拣选数");
            }
          }
        },
      ));
    } else {
      button0 = Container();
    }

    return Scaffold(
      appBar: AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0XFF00A5F1),
        title: Text(
          "自动拣选",
          style: TextStyle(fontSize: 18),
        ),
        elevation: 0,
        centerTitle: true,
      ),
      body: LoadBuilder(
          bgColor: Color(0x66BCBCBC),
          visibility: load,
          builder: Column(
            children: <Widget>[
              Expanded(
                child: Scrollbar(
                  child: SingleChildScrollView(
                    child: Column(
                      children: <Widget>[
                        TextItemCard(
                          name: "下架托盘码",
                          value: map['worktoolcode'] ?? "暂无",
                        ),
                        TextItemCard(
                          name: "下架库区",
                          value: map['areaname'] ?? "暂无",
                        ),
                        TextItemCard(
                          name: "下架货架",
                          value: map['shelfname'] ?? "暂无",
                        ),
                        TextItemCard(
                          name: "下架货位",
                          value: map['fromlocationcode'] ?? "暂无",
                        ),
                        Column(
                          children: <Widget>[
                            BaseMaterialInfo(
                              imgwid: ImageDownloadUtil.isImageType(image),
                              options: {
                                "materialname": "物资名称",
                                "materialcode": "实物ID",
                                "baseunitname": "基本单位",
                                "planquantity": "计划拣选数"
                              },
                              data: map,
                            ),
                            Container(
                              padding: EdgeInsets.all(15),
                              child: Column(
                                children: <Widget>[
                                  NumbercCountButton(
                                    height: 50,
                                    buttonWidth: 50,
                                    textEditingController: controller0,
                                    enabled: !shelfState,
                                    maxNumber: map["quantity"].toInt(),
                                      hintText: "输入实际拣选数"
                                  ),
                                 /* TextFieldItem(
                                          height: 50,
                                          scan: false,
                                        enabled: !shelfState,
                                          textEditingController: controller0,
                                          hintText: "输入实际拣选数"),*/
                                  Offstage(
                                    offstage: map["devicetypeid"] != "1",
                                    child: TextFieldItem(
                                        enabled: !shelfState,
                                        textEditingController: controller1,
                                        hintText: "扫描或输入拣选位置"),
                                  ),
                                ],
                              ),
                            )
                          ],
                        )
                      ],
                    ),
                  ),
                ),
              ),
              Row(
                children: <Widget>[
                  button,
                  Offstage(
                      offstage: false,
                      child: Container(
                          width: 1,
                          height: 30,
                          decoration: BoxDecoration(
                              border: Border.all(
                                  color: Color(0xffefefef), width: 1)))),
                  button0,
                ],
              )
            ],
          )),
    );
  }
}
